"""
集合推导式

格式：
    {expr for value in collection if condition}

"""

squared = {x**2 for x in [1, 1, 2]}
print(squared)

# 例1：用集合推到建字符串长度的集合

strings = ["a", "is", "with", "if", "file", "exception"]

T = {len(a) for a in strings}

print(T)

# 例2：一个由男人列表和女人列表组成的嵌套列表，取出姓名中带有两个以上字母e的姓名，组成列表

names = [['Tom', 'Billy', 'Jefferson', 'Andrew', 'Wesley', 'Steven', 'Joe'], ['Alice', 'Jill', 'Ana', 'Wendy', 'Jennifer', 'Sherry', 'Eva']]

# 用循环来实现

# result = []
#
# for list1 in names:
#     for name in list1:
#         if name.count('e') >= 2:
#             result.append(name)
#
# print(result)

# 用推导式来实现

result = [name for list1 in names for name in list1 if name.count('e') >= 2]
print(result)

result = [name for list1 in names for name in list1 if name.count('e') >= 2]
print(result)